package com.crack.spansframe;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.LruCache;

import com.crack.spansframe.extra.ICache;

/**
 * Created by liuyu on 16-4-20.
 */
public class DrawableCache extends LruCache<String, Drawable> {
    private static DrawableCache sCache;

    public static DrawableCache getInstance() {
        if (sCache == null) {
            sCache = new DrawableCache((int) (Runtime.getRuntime().maxMemory() / 2));
        }
        return sCache;
    }


    /**
     * @param maxSize for caches that do not override {@link #sizeOf}, this is
     *                the maximum number of entries in the cache. For all other caches,
     *                this is the maximum sum of the sizes of the entries in this cache.
     */
    public DrawableCache(int maxSize) {
        super(maxSize);
    }

    @Override
    protected int sizeOf(String key, Drawable value) {
        if (value instanceof BitmapDrawable) {
            BitmapDrawable drawable = (BitmapDrawable) value;
            Bitmap bitmap = drawable.getBitmap();
            return bitmap.getRowBytes() * bitmap.getHeight();
        }
        return value.getIntrinsicHeight() * value.getIntrinsicWidth() * 4;
    }


}
